/**
 * 
 */
package com.yst.metho.web.application.user.actions;

import javax.ejb.NoSuchObjectLocalException;

import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.GenericEditWindow;
import com.yst.metho.web.application.components.listingview.ToolbarAction;
import com.yst.metho.web.application.user.UserListModel;
import com.yst.metho.web.application.user.UserPane;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.data.user.User;

/**
 * @author perso
 * 
 */
public class EditUserAction extends ToolbarAction
{
	private UserListModel mModel;

	private User mSelectedUser = null;

	/**
	 * 
	 */
	private static final String ACTION_COMMAND = "EditUser";

	public EditUserAction(UserListModel model)
	{
		super(Right.EDIT_USER, "Edit");
		mModel = model;
		setActionCommand(ACTION_COMMAND);
		setEnabled(false);
		addActionListener(this);
	}

	/**
	 * Reaction aux differentes actions de l 'utilisateur(clic sur bouton,
	 * selection, ...)
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (ACTION_COMMAND.equals(e.getActionCommand()))
		{
			try
			{
				mSelectedUser.getLogin();
				UserPane view = new UserPane(mSelectedUser);
				GenericEditWindow window = new GenericEditWindow(view, "Modification d'un utilisateur", new ModifyUserBtn(mModel, view));
				window.setHeight(CreateUser.USER_VIEW_HEIGHT);
				SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
			}
			catch (NoSuchObjectLocalException nsee)
			{
				ExceptionMgt.displayMessageBox("L'utilisateur selectionne n'existe plus. La table va etre actualiser, si le probleme persiste contactez l'administrateur");
				mModel.refresh();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.ToolbarAction#stateChanged(nextapp.echo2.app.event.ChangeEvent)
	 */
	public void stateChanged(ChangeEvent arg0)
	{
		ListSelectionModel model = (ListSelectionModel) arg0.getSource();
		setEnabled(!model.isSelectionEmpty());
		if (!model.isSelectionEmpty())
		{
			int selectedIndex = model.getMinSelectedIndex();
			mSelectedUser = (User) mModel.getValueAt(0, selectedIndex);
		}
	}
}
